﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ClanAPI;

namespace WindowsFormsApplication1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        Clan CClan;
        Int32 Count;
        TankTotals total;

        private void bScan_Click(object sender, EventArgs e)
        {
            bScan.Enabled = false;
            tClanID.Enabled = false;
            lbMemberCount.Text = "Tokens/Members: -/-";
            lbTeirs.Text = "T10/T9/T8 Arty: -(-)/-(-)/-(-)";
            lbName.Text = "Name: ";
            PlayerTanks.DataSource = null;
            dgTanks.DataSource = null;
            cbPlayers.DataSource = null;
            pbload.Value = 0;
            pbload.Maximum = 100;
            CClan = new Clan(4, tClanID.Text);
            Count = 0;
            total = new TankTotals();
            CClan.PlayerCompleteChange += new Clan.GetCompleteHandler(CClan_PlayerCompleteChange);
            CClan.PlayerCountChange += new Clan.PlayerCountHandler(CClan_PlayerCountChange);
        }

        private void CClan_PlayerCountChange(Object Sender, Clan mClan)
        {
            if (Count == 0)
            {
                lbName.BeginInvoke(new Action(delegate()
                {
                    lbName.Text = "Name : " + mClan.Name;
                }));
                pbload.BeginInvoke(new Action(delegate()
                {
                    pbload.Maximum = mClan.TotalPlayers;
                }));
                lbMemberCount.BeginInvoke(new Action(delegate()
                {
                    lbMemberCount.Text = "Tokens/Members: -/" + mClan.TotalPlayers.ToString();
                }));
            }
            Count++;
            pbload.BeginInvoke(new Action(delegate()
            {
                pbload.Value = Count;
            }));
        }

        private void CClan_PlayerCompleteChange(Object Sender, Boolean AllComplete, Clan mClan)
        {
            if (AllComplete)
            {

                lbMemberCount.BeginInvoke(new Action(delegate()
                {
                    lbMemberCount.Text = "Tokens/Members: " + mClan.iTokens.ToString() + "/" + mClan.TotalPlayers.ToString();
                }));

                lbTeirs.BeginInvoke(new Action(delegate()
                {
                    lbTeirs.Text = "T10/T9/T8 Arty: " + mClan.T10Players + "(" + mClan.T10Tanks + ")/" + mClan.T9Players + "(" + mClan.T9Tanks + ")/" + mClan.T8APlayers + "(" + mClan.T8ATanks + ")";
                }));
                foreach (Player p in mClan.PlayerList)
                {
                    foreach (Tank t in p.Tanks)
                    {
                        total.UpdateTank(t);
                    }
                }
                total.TotalTanks.Sort(delegate(TankTotals.sTank cust1, TankTotals.sTank cust2)
                {
                    return Comparer<int>.Default.Compare(cust1.Level, cust2.Level);
                });
                total.TotalTanks.Reverse();
                dgTanks.BeginInvoke(new Action(delegate(){
                    dgTanks.DataSource = total.TotalTanks;
                }));
                bScan.BeginInvoke(new Action(delegate(){
                    bScan.Enabled = true;
                }));
                tClanID.BeginInvoke(new Action(delegate()
                {
                    tClanID.Enabled = true;
                }));
                MyClan.BeginInvoke(new Action(delegate()
                {
                    MyClan.Enabled = true;
                }));
                cbPlayers.BeginInvoke(new Action(delegate()
                {
                    cbPlayers.DataSource = mClan.PlayerList.Select(tmp => tmp.Name).ToArray();
                }));
            }
        }

        private void MainForm_FormClosing(object sender, EventArgs e)
        {
        }

        private void cbPlayers_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                String Name = ((String)cbPlayers.SelectedItem);
                Player Tmp = CClan.PlayerList.Find(delegate(Player PL)
                {
                    return PL.Name == Name;
                }
                );
                PlayerTanks.DataSource = Tmp.Tanks;
            }
            catch { }
        }

        private void MyClan_Click(object sender, EventArgs e)
        {
            MyClan.Enabled = false;
            tClanID.Text = "168";
            bScan.PerformClick();
        }
    }
}
